package com.any.hook.core.webapi.mapper;

import com.any.hook.core.webapi.entity.JobInfo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface JobInfoMapper {
    @Insert("insert into tb_jobinfo(id,name,context,scheduler,stat,run_time) values(" +
            "#{jobinfo.id,jdbcType=BIGINT},#{jobinfo.name,jdbcType=VARCHAR},#{jobinfo.context,jdbcType=VARCHAR}," +
            "#{jobinfo.scheduler,jdbcType=VARCHAR}," +
            "#{jobinfo.stat,jdbcType=INTEGER},#{jobinfo.runTime,jdbcType=INTEGER})")
    int insertInfo(@Param("jobinfo") JobInfo jobInfo);

    @Update("update tb_jobinfo set context=#{jobinfo.context} " +
            "where id=#{jobinfo.id}")
    int updateInfo(@Param("jobinfo") JobInfo jobInfo);

    @Select("select id,name,context,scheduler,stat,run_time from tb_jobinfo limit #{pagesize} offset #{skipcount}")
    @Results({
            @Result(property ="id" ,column ="id" ),
            @Result(property = "name", column = "name"),
            @Result(property = "context", column = "context"),
            @Result(property = "scheduler", column = "scheduler"),
            @Result(property = "stat",column = "stat"),
            @Result(property = "runTime",column = "run_time")
    })
    List<JobInfo> queryAll(@Param("skipcount") int skipcount, @Param("pagesize") int pagesize);


    @Select("select * from tb_jobinfo where id=#{id}")
    @Results({
            @Result(property ="id" ,column ="id" ),
            @Result(property = "context", column = "context"),
            @Result(property = "scheduler", column = "scheduler"),
            @Result(property = "stat",column = "stat"),
            @Result(property = "runTime",column = "run_time")
    })
    JobInfo queryById(@Param("id") Long id);
    @Select("select id,context from tb_jobinfo where id=#{id}")
    @Results({
            @Result(property ="id" ,column ="id" ),
            @Result(property = "context", column = "context")
    })
    JobInfo queryContextById(@Param("id") Long id);

    @Delete("delete from tb_jobinfo where id=#{id}")
    int deleteById(@Param("id") Long id);

    @Select("select count(*) from tb_jobinfo")
    int selectCount();
    @Update("update tb_jobinfo set " +
            "stat=1," +
            "run_time=run_time+1" +
            "where id=#{id} and stat!=1")
    int changeStartStat(@Param("id") Long id);
    @Update("update tb_jobinfo set " +
            "stat=0" +
            "where id=#{id}")
    int changeStopStat(@Param("id") Long id);
}
